/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snmpapplication.snmp;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import snmpapplication.Main;
import snmpapplication.log.Severity;

/**
 *
 * @author APetcu
 */
public class SnmpClientSet {

    public static boolean snmpSet(String host, String community, String strOID, String Value) {
        host = host + "/" + "161";
        Address tHost = GenericAddress.parse(host);
        Snmp snmp;
        try {
            TransportMapping transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            transport.listen();
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            target.setAddress(tHost);
            target.setRetries(2);
            target.setTimeout(3000);
            target.setVersion(SnmpConstants.version1); //Set the correct SNMP version here
            PDU pdu = new PDU();
            //Depending on the MIB attribute type, appropriate casting can be done here
            pdu.add(new VariableBinding(new OID(strOID), new OctetString(Value)));
            pdu.setType(PDU.SET);
            ResponseListener listener = new ResponseListener() {

                @Override
                public void onResponse(ResponseEvent event) {
                    PDU strResponse;
                    String result;
                    ((Snmp) event.getSource()).cancel(event.getRequest(), this);
                    strResponse = event.getResponse();
                    if (strResponse != null) {
                        result = strResponse.getErrorStatusText();
                        //System.out.println("Set Status is: " + result);
                        Main.log.log(new Severity(Severity.SEV_INFO), "SNMP SET status is " + result);
                    }
                }
            };
            snmp.send(pdu, target, null, listener);
            snmp.close();
            Main.log.log(new Severity(Severity.SEV_INFO), "SNMP SET succesfull");
            return true;
        } catch (Exception e) {
            Main.log.log(new Severity(Severity.SEV_ERR), "Exception at SNMP SET:" + e.getMessage());
            return false;
        }
    }

    public static boolean snmpSet(String host, String community, String strOID, Integer Value) {
        host = host + "/" + "161";
        Address tHost = GenericAddress.parse(host);
        Snmp snmp;
        try {
            TransportMapping transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            transport.listen();
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            target.setAddress(tHost);
            target.setRetries(2);
            target.setTimeout(3000);
            target.setVersion(SnmpConstants.version1); //Set the correct SNMP version here
            PDU pdu = new PDU();
            //Depending on the MIB attribute type, appropriate casting can be done here
            pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value)));
            pdu.setType(PDU.SET);
            ResponseListener listener = new ResponseListener() {

                @Override
                public void onResponse(ResponseEvent event) {
                    PDU strResponse;
                    String result;
                    ((Snmp) event.getSource()).cancel(event.getRequest(), this);
                    strResponse = event.getResponse();
                    if (strResponse != null) {
                        result = strResponse.getErrorStatusText();
                        //System.out.println("Set Status is: " + result);
                        Main.log.log(new Severity(Severity.SEV_INFO), "SNMP SET status is " + result);
                    }
                }
            };
            snmp.send(pdu, target, null, listener);
            snmp.close();
            Main.log.log(new Severity(Severity.SEV_INFO), "SNMP SET succesfull");
            return true;
        } catch (Exception e) {
            Main.log.log(new Severity(Severity.SEV_ERR), "Exception at SNMP SET:" + e.getMessage());
            return false;
        }
    }
}
